package com.xframe.domain.views.builder;

import cn.hutool.json.JSONUtil;
import com.xframe.domain.emnus.Constant;
import com.xframe.domain.emnus.PublicFieldEnum;
import com.xframe.domain.role.IUserCredentials;
import com.xframe.domain.sys.SysModel;
import com.xframe.domain.sys.SysModelAttr;
import com.xframe.domain.views.ui.*;
import com.xframe.xdal.core.annotation.XQueryCondition;
import com.xframe.xdal.core.annotation.XSelectItemCollect;
import com.xframe.xdal.core.model.Operator;
import com.xframe.xdal.core.model.mapping.IRelation;
import com.xframe.xdal.core.model.mapping.TableInfoBuilder;
import com.xframe.xdal.core.util.StringUtil;
import com.xframe.xdal.core.util.XReflect;
import lombok.extern.log4j.Log4j2;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description: 生成实体的查询条件视图
 * @ClassName: QueryUIModelBuilder
 * @Author: xufeng
 * @CreateTime: 2023/12/06
 **/
@Log4j2
public class QueryUIModelBuilder extends UIModelBuilder {

    static final String VIEW_NAME = "queryView";
    static final String CONFIG_TYPE = "query";
    static final String RENDER_CLASS = "queryView";

    @Override
    public String getViewName() {
        return VIEW_NAME;
    }

    @Override
    public String getConfigType() {
        return CONFIG_TYPE;
    }

    @Override
    public String getRenderClass() {
        return RENDER_CLASS;
    }



    @Override
    public String setSysModelViewConfigContent(SysModel sysModel, IUserCredentials userCredentials) {

        Map<String,XQueryCondition> map = XReflect.getFieldAnnotationMap(sysModel.getModelDataType(), XQueryCondition.class);


        // 找出所有的非公有字段
        List<SysModelAttr> sysModelAttrList = sysModel.getModelAttrList();
        List<QueryUIModel> queryUIList = new ArrayList<>();
        for(SysModelAttr modelAttr : sysModelAttrList){
            if(!map.containsKey(modelAttr.getAttrName())){
                continue;
            }
            XQueryCondition queryCondition = map.get(modelAttr.getAttrName());
            // 判断是不是引用字段
            IRelation relation = findRefAttr(modelAttr,sysModel);
            String attrType = modelAttr.getType();
            if(relation != null){
                // 是引用类型 需要生成下拉框的查询条件
                QueryUIModel selectUIModel = new QueryUIModel();
                selectUIModel.setAttrName(modelAttr.getAttrName());
                selectUIModel.setProp(modelAttr.getAttrName());
                selectUIModel.setDefValue(StringUtil.EMPTY);
                selectUIModel.setLabel(QueryUIModel.NAME_SIGE + modelAttr.getAttrName());
                selectUIModel.setPlaceholder(QueryUIModel.NAME_SIGE + modelAttr.getAttrName() + QueryUIModel.NAME_SIGE_HOLDER);
                selectUIModel.setRemark(modelAttr.getDesc());
                selectUIModel.setComponentName(UIType.SELECT.getComponentName());
                selectUIModel.setDisabled(false);
                selectUIModel.setMultiple(false);

                selectUIModel.setShow(true);
                selectUIModel.setOp(Operator.EQ.name());

                DataSourceOption dataSourceOption = new DataSourceOption();
                dataSourceOption.setDataSourceCode(relation.getDataType().getName());

                //XSelectItemCollect xSelectItemCollect = TableInfoBuilder.findXSelectItem(relation.getDataType());
                dataSourceOption.setOptionType(OptionType.DYNAMICS.getValue());

                dataSourceOption.setOptionName(XSelectItemCollect.OPTION_NAME);
                dataSourceOption.setOptionId(XSelectItemCollect.OPTION_ID);

                dataSourceOption.setQueryKey(QueryUIModel.SELECT);
                dataSourceOption.setFilter(QueryUIModel.DELFLAG_FILTER);
                if(StringUtil.isNotEmpty(queryCondition.valueCode())){
                    dataSourceOption.setFilter(QueryUIModel.DELFLAG_FILTER + ";valCode:EQ:" + queryCondition.valueCode());
                }
                dataSourceOption.setSorts(queryCondition.sorts());
                selectUIModel.setDataSourceOption(dataSourceOption);
                queryUIList.add(selectUIModel);
            }else {
                // 判断类型 只生成String 和 Date类型的 查询条件 日期类型的需要生成一对 起始
                if(attrType.equals(String.class.getName())) {
                    QueryUIModel queryUIModel = new QueryUIModel();
                    queryUIModel.setAttrName(modelAttr.getAttrName());
                    queryUIModel.setProp(modelAttr.getAttrName());

                    queryUIModel.setDefValue(StringUtil.EMPTY);
                    queryUIModel.setLabel(QueryUIModel.NAME_SIGE + modelAttr.getAttrName());
                    queryUIModel.setPlaceholder(QueryUIModel.NAME_SIGE + modelAttr.getAttrName() + QueryUIModel.NAME_SIGE_HOLDER);
                    queryUIModel.setRemark(modelAttr.getDesc());
                    queryUIModel.setComponentName(UIType.TEXT.getComponentName());

                    queryUIModel.setDisabled(false);
                    queryUIModel.setMultiple(false);
                    queryUIModel.setDataSourceOption(null);
                    queryUIModel.setShow(true);
                    queryUIModel.setOp(Operator.LIKE.name());
                    queryUIModel.setUpdataFlag(true);
                    queryUIList.add(queryUIModel);
                }
                if(attrType.equals(java.util.Date.class.getName())){
                    // 日期类型的需要生成一对 起始条件
                    QueryUIModel begQueryUIModel = new QueryUIModel();
                    begQueryUIModel.setAttrName(modelAttr.getAttrName());
                    begQueryUIModel.setProp(modelAttr.getAttrName() + QueryUIModel.START);
                    begQueryUIModel.setDefValue(DefValueEnum.GET_MONTH_FIRSTDAY.getName());
                    begQueryUIModel.setLabel(QueryUIModel.NAME_SIGE + modelAttr.getAttrName() + QueryUIModel.START);
                    begQueryUIModel.setPlaceholder(QueryUIModel.NAME_SIGE + modelAttr.getAttrName() + QueryUIModel.START_HOLDER);
                    begQueryUIModel.setRemark(modelAttr.getDesc());
                    begQueryUIModel.setComponentName(UIType.DATE.getComponentName());

                    begQueryUIModel.setDisabled(false);
                    begQueryUIModel.setMultiple(false);
                    begQueryUIModel.setDataSourceOption(null);
                    begQueryUIModel.setShow(true);
                    // 大于等于
                    begQueryUIModel.setOp(Operator.GE.name());
                    begQueryUIModel.setUpdataFlag(true);

                    QueryUIModel endQueryUIModel = new QueryUIModel();
                    endQueryUIModel.setAttrName(modelAttr.getAttrName());
                    endQueryUIModel.setProp(modelAttr.getAttrName() + QueryUIModel.END);
                    endQueryUIModel.setDefValue(DefValueEnum.NOW_TIME.getName());
                    endQueryUIModel.setLabel(QueryUIModel.NAME_SIGE + modelAttr.getAttrName() + QueryUIModel.END);
                    endQueryUIModel.setPlaceholder(QueryUIModel.NAME_SIGE + modelAttr.getAttrName() + QueryUIModel.END_HOLDER);
                    endQueryUIModel.setRemark(modelAttr.getDesc());
                    endQueryUIModel.setComponentName(UIType.DATE.getComponentName());

                    endQueryUIModel.setDisabled(false);
                    endQueryUIModel.setMultiple(false);
                    endQueryUIModel.setDataSourceOption(null);
                    endQueryUIModel.setShow(true);
                    // 大于等于
                    endQueryUIModel.setOp(Operator.LE.name());
                    endQueryUIModel.setUpdataFlag(true);

                    queryUIList.add(begQueryUIModel);
                    queryUIList.add(endQueryUIModel);
                }

                if(attrType.equals(Integer.class.getName()) && modelAttr.getAttrName().equals(PublicFieldEnum.DEL_FLAG.getValue())){
                    // 删除标记
                    QueryUIModel delUIModel = new QueryUIModel();
                    delUIModel.setAttrName(modelAttr.getAttrName());
                    delUIModel.setProp(modelAttr.getAttrName());
                    delUIModel.setDefValue(StringUtil.EMPTY);
                    delUIModel.setLabel(QueryUIModel.NAME_SIGE + modelAttr.getAttrName());
                    delUIModel.setPlaceholder(QueryUIModel.NAME_SIGE + modelAttr.getAttrName() + QueryUIModel.NAME_SIGE_HOLDER);
                    delUIModel.setRemark(StringUtil.EMPTY);
                    delUIModel.setComponentName(UIType.SELECT.getComponentName());
                    delUIModel.setDisabled(false);
                    delUIModel.setMultiple(false);
                    delUIModel.setShow(true);
                    delUIModel.setOp(Operator.EQ.name());
                    DataSourceOption dataSourceOption = new DataSourceOption();
                    dataSourceOption.setDataSourceCode(PublicFieldEnum.DEL_FLAG.getValue());
                    dataSourceOption.setOptionType(OptionType.STATIC.getValue());
                    dataSourceOption.setQueryKey(StringUtil.EMPTY);
                    delUIModel.setDataSourceOption(dataSourceOption);
                    queryUIList.add(delUIModel);
                }
            }

        }

        QueryBill queryBill = new QueryBill();
        queryBill.setElements(queryUIList);
        queryBill.setVer("v1");
        queryBill.setConfigType(getConfigType());
        String json = JSONUtil.toJsonStr(queryBill);
        return json;
    }

}
